// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package redshift;

import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.*;
import software.amazon.awssdk.services.ses.model.Body;

import javax.mail.MessagingException;

public class SendMessage {

        public static void sendMessage(SesClient client,
                        String sender,
                        String recipient,
                        String subject,
                        String bodyHTML) throws MessagingException {

                Destination destination = Destination.builder()
                                .toAddresses(recipient)
                                .build();

                Content content = Content.builder()
                                .data(bodyHTML)
                                .build();

                Content sub = Content.builder()
                                .data(subject)
                                .build();

                Body body = Body.builder()
                                .html(content)
                                .build();

                software.amazon.awssdk.services.ses.model.Message msg = Message.builder()
                                .subject(sub)
                                .body(body)
                                .build();

                SendEmailRequest emailRequest = SendEmailRequest.builder()
                                .destination(destination)
                                .message(msg)
                                .source(sender)
                                .build();

                try {
                        System.out.println("Attempting to send an email through Amazon SES "
                                        + "using the AWS SDK for Java...");
                        client.sendEmail(emailRequest);

                } catch (SesException e) {
                        System.err.println(e.awsErrorDetails().errorMessage());
                        System.exit(1);
                }
        }
}
